ChatCommands = { }

--Usage:
--Chat command, Callback, Should the prefix stay constant?
function AddChatCommand(cmd, callback, caller)
	table.insert(ChatCommands, { cmd = cmd, callback = callback, caller = caller })
end

function GM:PlayerSay(ply, text, team)
	ply:SetAFK(false)
	self.BaseClass:PlayerSay(ply, text)

	local ftext = string.lower(text)
	
	for k, v in pairs(ChatCommands) do
	
		local endpos = string.len(v.cmd)+1
		bump = false
		if string.len(ftext)<endpos then endpos = endpos-1 bump=true end
		local strcmd = string.sub(ftext, 1, endpos)

		local argstart = 1
		
		if(string.sub(text, endpos , endpos ) == " ") then
			argstart = 2
		end
		
		if(strcmd == v.cmd.." " && !bump) then
			return v.callback(ply, string.sub(text, string.len(v.cmd) + argstart) or "", v.caller)
		elseif (strcmd==v.cmd && bump) then
			return v.callback(ply, string.sub(text, string.len(v.cmd) + argstart) or "", v.caller)
		end
	
	end
	
	return text
end

function AM(ply, args)
	msg = Purify(args)
	for k, v in pairs(player.GetAll()) do
		if (IsAllied(v, ply)) then
			umsg.Start("allies_chat", v)
			umsg.String(msg)
			umsg.Entity(ply)
			umsg.End()
		end
	end
	umsg.Start("allies_chat", ply)
   	umsg.String(msg)
	umsg.Entity(ply)
	umsg.End()
	return ""
end
AddChatCommand("/a", AM)
 
------------------------------------
-- Are they using teamchat? if so 
-- only send it to their Allies.
------------------------------------
function TeamChats(ply, text, team)
    if (team == false) then
		AM(ply, text)
        return ""
    end
end
hook.Add("PlayerSay", "TeamChats", TeamChats)

------------------------------------
-- Private Messages.
------------------------------------
function PM(ply, args)
	args = Purify(args)
	local namepos = string.find(args, " ")
	if(not namepos) then return "" end
	
	local name = string.sub(args, 1, namepos - 1)
	local msg = string.sub(args, namepos + 1)
	
	target = FindPlayer(name)
		
	if(target) then
			umsg.Start("private_chat", target)
			umsg.String(msg)
			umsg.Entity(ply)
			umsg.End()
			umsg.Start("private_chat", ply)
			umsg.String(msg)
			umsg.Entity(ply)
			umsg.End()
	else
		
		Notify(ply, 4, 3, "Could not find player: " .. name)
		
	end
	
	return ""

end
AddChatCommand("/pm", PM)